//
//  BusinessInfoCollectViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/19.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"
#import "TT_UserInfoCollectManager.h"
#import "BusinessInfoCollectHeadView.h"

#import "BusinessInfoCollectCell.h"
#import "BusinessInfoCollectTextCell.h"

#import "BusinessInfoCollectViewController.h"

@interface BusinessInfoCollectViewController ()<UITextFieldDelegate,UICollectionViewDataSource, UICollectionViewDelegate,ZZLayoutDelegate>

/**页面主视图*/
@property (nonatomic , strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray <BusinessInfoCollectModel *>*projArray;

@property (nonatomic, strong) NSMutableArray <BusinessInfoCollectModel *>*majorArray;

@property (nonatomic, strong) NSMutableArray <BusinessInfoCollectModel *>*budgetArray;

@property (weak, nonatomic) IBOutlet UIButton *handleBtn;
@property (weak, nonatomic) IBOutlet UIButton *imBtn;


@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;


@property (weak, nonatomic) IBOutlet UICollectionView *collectionview;

@end

@implementation BusinessInfoCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self prepareData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2 border:1 color:UIColorHex(0xff4242)];
        [self.imBtn pp_clipCorners:UIRectCornerAllCorners radius:self.imBtn.frame.size.height/2];

        [self.jumpBtn pp_clipCorners:UIRectCornerAllCorners radius:self.jumpBtn.frame.size.height/2];

    });
}

- (void)initView {
    
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.view.backgroundColor = [UIColor whiteColor];
    
    ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeAutomateFloat delegate:self];
    
    self.collectionview.collectionViewLayout = layout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    
    //注册cell
    [self.collectionview registerNib:[UINib nibWithNibName:@"BusinessInfoCollectTextCell" bundle:nil] forCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID"];
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"BusinessInfoCollectCell" bundle:nil] forCellWithReuseIdentifier:@"BusinessInfoCollectCellID"];

    
    [self.collectionview registerNib:[UINib nibWithNibName:@"BusinessInfoCollectHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BusinessInfoCollectHeadView"];
    
    
    [self.collectionview registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
    
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.bounces = YES;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if (@available(iOS 11.0, *)) {
        self.collectionview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.collectionview.backgroundColor = [UIColor whiteColor];
    
}

- (IBAction)imHandleAction:(id)sender {
    
    NSString *str = @"";
    
    str = [self handleStr];
    if (str.length > 0) {
        [TT_UserInfoCollectManager saveUserInfoWithType:UserInfoCollectType_UnFinishProj AndInfo:str];
    }
    
    //因为只有登录才会弹出，所以直接提交
    [TT_UserInfoCollectManager uploadUserInfoWithType:UserInfoCollectType_UnFinishProj];
    
    NSString *majorStr = @"";
    for (int i = 0; i < self.majorArray.count; i++) {
        if (self.majorArray[i].isSel) {
            majorStr = [majorStr stringByAppendingString:self.majorArray[i].title];
        }
    }
    
    NSString *prostr = @"";
    for (int i = 0; i < self.projArray.count; i++) {
        
        if (self.projArray[i].isSel) {
            prostr = [prostr stringByAppendingFormat:@"%@、",self.projArray[i].title];
        }
    }
    NSString *projStr = @"";
    if (prostr.length > 0) {
        projStr = [prostr substringWithRange:NSMakeRange(0, [prostr length] - 1)];
    }
    
    NSString *moneyStr = @"";
    for (int i = 0; i < self.budgetArray.count; i++) {
        
        if (self.budgetArray[i].isSel) {
            moneyStr = [moneyStr stringByAppendingString:self.budgetArray[i].title];
        }
    }
    
    NSString *resultStr = [NSString stringWithFormat:@"你好，我是“%@”，我还有“%@”项目未完成，我的预算为“%@”，我需要专属定制服务",majorStr,projStr,moneyStr];
    
    if (self.completion) {
        self.completion(YES,YES,resultStr);
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)handleAction:(id)sender {
    
    NSString *str = @"";
    
    str = [self handleStr];
    if (str.length > 0) {
        [TT_UserInfoCollectManager saveUserInfoWithType:UserInfoCollectType_UnFinishProj AndInfo:str];
    }
    
    //因为只有登录才会弹出，所以直接提交
    [TT_UserInfoCollectManager uploadUserInfoWithType:UserInfoCollectType_UnFinishProj];
        
    if (self.completion) {
        self.completion(YES,YES,@"");
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSString *)handleStr {
    
    NSString *handleStr = [NSString stringWithFormat:@"%@;%@;%@;",[self majorStr],[self projStr],[self moneyStr]];
    return handleStr;
}

- (NSString *)majorStr {

    NSString *majorStr = @"我是:";
    for (int i = 0; i < self.majorArray.count; i++) {
        
        if (self.majorArray[i].isSel) {
            majorStr = [majorStr stringByAppendingString:self.majorArray[i].title];
        }
    }
    return majorStr;
}

- (NSString *)projStr {

    NSString *str = @"项目:";
    for (int i = 0; i < self.projArray.count; i++) {
        
        if (self.projArray[i].isSel) {
            str = [str stringByAppendingFormat:@"%@,",self.projArray[i].title];
        }
    }
    NSString *projStr = @"";
    if (str.length > 0) {
        projStr = [str substringWithRange:NSMakeRange(0, [str length] - 1)];
    }
    return projStr;
}

- (NSString *)moneyStr {

    NSString *moneyStr = @"项目预算:";
    for (int i = 0; i < self.budgetArray.count; i++) {
        
        if (self.budgetArray[i].isSel) {
            moneyStr = [moneyStr stringByAppendingString:self.budgetArray[i].title];
        }
    }
    return moneyStr;
}


- (IBAction)jumpAction:(id)sender {
    
    if (self.completion) {
        self.completion(YES,NO,@"");
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark- 协议方法
//collectionView的协议方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case BusinessInfoSectionType_major:{

            for (int i = 0; i < self.majorArray.count; i++) {
                self.majorArray[i].isSel = NO;
            }
            self.majorArray[indexPath.row].isSel = YES;
            [self.collectionview reloadData];
            
        } break;
        case BusinessInfoSectionType_proj:{

            self.projArray[indexPath.row].isSel = !self.projArray[indexPath.row].isSel;
            [self.collectionview reloadData];
            
        } break;
        case BusinessInfoSectionType_money:{
            
            for (int i = 0; i < self.budgetArray.count; i++) {
                self.budgetArray[i].isSel = NO;
            }
            
            self.budgetArray[indexPath.row].isSel = YES;
            [self.collectionview reloadData];

        } break;
        default:
            break;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    BusinessInfoCollectTextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID" forIndexPath:indexPath];
    switch (indexPath.section) {
        case BusinessInfoSectionType_major:{
            
            BusinessInfoCollectTextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID" forIndexPath:indexPath];
            [cell fillCellWithModel:self.majorArray[indexPath.row]];

            return cell;

        } break;
        case BusinessInfoSectionType_proj:{
            
            BusinessInfoCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BusinessInfoCollectCellID" forIndexPath:indexPath];
            [cell fillCellWithModel:self.projArray[indexPath.row]];
            return cell;

        } break;
        case BusinessInfoSectionType_money:{
            
            BusinessInfoCollectTextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID" forIndexPath:indexPath];
            [cell fillCellWithModel:self.budgetArray[indexPath.row]];

            return cell;

        } break;
        default:
            break;
    }
    
    BusinessInfoCollectTextCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID" forIndexPath:indexPath];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (section) {
        case BusinessInfoSectionType_major:{
            return self.majorArray.count;
        } break;
        case BusinessInfoSectionType_proj:{
            return self.projArray.count;
        } break;
        case BusinessInfoSectionType_money:{
            return self.budgetArray.count;
        } break;
        default:
            break;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        BusinessInfoCollectHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BusinessInfoCollectHeadView" forIndexPath:indexPath];
        
        switch (indexPath.section) {
            case BusinessInfoSectionType_major:{
                headerView.sectionTitle.text = @"我是";
                
            } break;
            case BusinessInfoSectionType_proj:{
                headerView.sectionTitle.text = @"未完成项目";
                
            } break;
            case BusinessInfoSectionType_money:{
                headerView.sectionTitle.text = @"未完成项目预算";

            } break;
            default:
                break;
        }
        
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的度
    
    switch (indexPath.section) {
        case BusinessInfoSectionType_major:{
            return 30;//这里可以根据内容传入任意宽度

        } break;
        case BusinessInfoSectionType_proj:{
            return (TTScreenWidth - 50)/3;//同一section下请不要改变高度.

        } break;
        case BusinessInfoSectionType_money:{
            return 30;//这里可以根据内容传入任意宽度

        } break;
        default:
            break;
    }
    return 30;//这里可以根据内容传入任意宽度
}

- (CGFloat)layout:(ZZLayout *)layout widthForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的宽
    
    return (TTScreenWidth - 50)/3;//这里可以根据内容传入任意宽度
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距
    return 10;
}

- (CGFloat) layout:(ZZLayout *)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距
    return 10;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度
    return CGSizeMake(self.view.bounds.size.width, 44);
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {
    return [UIColor whiteColor];
}

#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeAutomateFloat delegate:self];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kStatusBarHeight - NavBar_Height) collectionViewLayout:layout];
        _collectionView.delegate = self;_collectionView.dataSource = self;
        
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"BusinessInfoCollectTextCell" bundle:nil] forCellWithReuseIdentifier:@"BusinessInfoCollectTextCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"BusinessInfoCollectCell" bundle:nil] forCellWithReuseIdentifier:@"BusinessInfoCollectCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"BusinessInfoCollectHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BusinessInfoCollectHeadView"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = YES;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor whiteColor];
        
    }
    return _collectionView;
}

- (NSMutableArray<BusinessInfoCollectModel *> *)majorArray {
    if (!_majorArray) {
        _majorArray = @[].mutableCopy;
    }
    return _majorArray;
}

- (NSMutableArray <BusinessInfoCollectModel *>*)projArray {
    if (!_projArray) {
        _projArray = @[].mutableCopy;
    }
    return _projArray;
}

- (NSMutableArray <BusinessInfoCollectModel *>*)budgetArray {
    if (!_budgetArray) {
        _budgetArray = @[].mutableCopy;
    }
    return _budgetArray;
}

- (void)prepareData {
    
    NSArray *majorArray = @[
        @{@"title": @"新娘",
          @"img": @"",
          @"isSel":@"1",
        },
        @{@"title": @"新郎",
          @"img": @"",
          @"isSel":@"0",
        },
        @{@"title": @"亲友团",
          @"img": @"",
          @"isSel":@"0",
        }
    ];
    
    NSArray *projData = @[
        @{@"title": @"婚戒定制",
          @"img": B_INFO_COLLECT_HJDZ,
          @"localImg": @"gather_project_1.webp",
          @"isSel":@"0",
        },
        @{@"title": @"四大金刚",
          @"img": B_INFO_COLLECT_SDJG,
          @"localImg": @"gather_project_2.webp",
          @"isSel":@"0",
        },
        @{@"title": @"预订酒店",
          @"img": B_INFO_COLLECT_YDJD,
          @"localImg": @"gather_project_3.webp",
          @"isSel":@"0",
        },
        @{@"title": @"婚礼策划",
          @"img": B_INFO_COLLECT_HLCH,
          @"localImg": @"gather_project_4.webp",
          @"isSel":@"0",
        },
        @{@"title": @"婚纱摄影",
          @"img": B_INFO_COLLECT_HSSY,
          @"localImg": @"gather_project_5.webp",
          @"isSel":@"0",
        },
        @{@"title": @"婚纱礼服",
          @"img": B_INFO_COLLECT_HSLF,
          @"localImg": @"gather_project_6.webp",
          @"isSel":@"0",
        },
        @{@"title": @"男士礼服",
          @"img": B_INFO_COLLECT_NSLF,
          @"localImg": @"gather_project_7.webp",
          @"isSel":@"0",
        },
        @{@"title": @"婚车预订",
          @"img": B_INFO_COLLECT_HCYD,
          @"localImg": @"gather_project_8.webp",
          @"isSel":@"0",
        },
        @{@"title": @"喜糖定制",
          @"img": B_INFO_COLLECT_XTDZ,
          @"localImg": @"gather_project_9.webp",
          @"isSel":@"0",
        }
    ];
    
    NSArray *moneyArray = @[
        @{@"title": @"一万以下",
          @"img": @"",
          @"isSel":@"1",
        },
        @{@"title": @"一万~三万",
          @"img": @"",
          @"isSel":@"0",
        },
        @{@"title": @"三万~六万",
          @"img": @"",
          @"isSel":@"0",
        },
        @{@"title": @"六万~十万",
          @"img": @"",
          @"isSel":@"0",
        },
        @{@"title": @"十万以上",
          @"img": @"",
          @"isSel":@"0",
        }
    ];
    
    for (int i = 0; i < majorArray.count; i++) {
        
        NSDictionary *dic = majorArray[i];
        BusinessInfoCollectModel *model = [BusinessInfoCollectModel modelWithDictionary:dic];
        [self.majorArray addObject:model];
    }
    
    for (int i = 0; i < projData.count; i++) {
        
        NSDictionary *dic = projData[i];
        BusinessInfoCollectModel *model = [BusinessInfoCollectModel modelWithDictionary:dic];
        [self.projArray addObject:model];
    }
    
    for (int i = 0; i < moneyArray.count; i++) {
        
        NSDictionary *dic = moneyArray[i];
        BusinessInfoCollectModel *model = [BusinessInfoCollectModel modelWithDictionary:dic];
        [self.budgetArray addObject:model];
    }
    
    [self.collectionview reloadData];
}

@end
